package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.pojo.vo.CategorySimpleListVO;
import com.xbongbong.paas.pojo.vo.DepartmentSimpleListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.productcategory.pojo.dto.ProductCategoryAddDTO;
import com.xbongbong.pro.productcategory.pojo.dto.ProductCategoryDeleteDTO;
import com.xbongbong.pro.productcategory.pojo.dto.ProductCategoryListDTO;
import com.xbongbong.pro.productcategory.pojo.dto.ProductCategoryPropertyDTO;
import com.xbongbong.pro.productcategory.pojo.dto.ProductCategorySortDTO;
import com.xbongbong.pro.productcategory.pojo.dto.ProductCategoryUpdateDTO;
import com.xbongbong.pro.productcategory.pojo.vo.ProductCategoryAddVO;
import com.xbongbong.pro.productcategory.pojo.vo.ProductCategoryDeleteVO;
import com.xbongbong.pro.productcategory.pojo.vo.ProductCategoryListVO;
import com.xbongbong.pro.productcategory.pojo.vo.ProductCategoryPropertyVO;
import com.xbongbong.pro.productcategory.pojo.vo.ProductCategorySortVO;
import com.xbongbong.pro.productcategory.pojo.vo.ProductCategoryUpdateVO;
import com.xbongbong.saas.service.ProductCategoryService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/3/22 10:17
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/product/category")
public class ProductCategoryController {

    @Resource
    private ProductCategoryService productCategoryService;

    /**
     * 产品分类
     * @param productCategoryListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid ProductCategoryListDTO productCategoryListDTO, BindingResult br) throws Exception {
        XbbResponse<ProductCategoryListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductCategoryListVO productCategoryListVO = productCategoryService.list(productCategoryListDTO);
                response = new XbbResponse<>(productCategoryListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 表单下的产品分类
     * @param paasFormDataEntity
     * @author weiming
     * @date 2020/5/26 19:49
     * @return String
     */
    @RequestMapping(value = "/formProductCategoryList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formProductCategoryList(@RequestBody PaasFormDataEntity paasFormDataEntity) {
        XbbResponse<CategorySimpleListVO> response;
            try {
                CategorySimpleListVO categorySimpleListVO = productCategoryService.formProductCategoryList(paasFormDataEntity.getCorpid());
                response = new XbbResponse<>(categorySimpleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        return JSON.toJSONString(response);
    }

    /**
     * 产品分类排序
     * @param productCategorySortDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid ProductCategorySortDTO productCategorySortDTO, BindingResult br) throws Exception {
        XbbResponse<ProductCategorySortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductCategorySortVO productCategorySortVO = productCategoryService.sort(productCategorySortDTO);
                response = new XbbResponse<>(productCategorySortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 新建产品分类
     * @param productCategoryAddDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid ProductCategoryAddDTO productCategoryAddDTO, BindingResult br) throws Exception {
        XbbResponse<ProductCategoryAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductCategoryAddVO productCategoryAddVO = productCategoryService.add(productCategoryAddDTO);
                response = new XbbResponse<>(productCategoryAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 更新产品分类
     * @param productCategoryUpdateDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid ProductCategoryUpdateDTO productCategoryUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<ProductCategoryUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductCategoryUpdateVO productCategoryUpdateVO = productCategoryService.update(productCategoryUpdateDTO);
                response = new XbbResponse<>(productCategoryUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除产品分类
     * @param productCategoryDeleteDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid ProductCategoryDeleteDTO productCategoryDeleteDTO, BindingResult br) throws Exception {
        XbbResponse<ProductCategoryDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductCategoryDeleteVO productCategoryDeleteVO = productCategoryService.delete(productCategoryDeleteDTO);
                response = new XbbResponse<>(productCategoryDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取分类属性设置
     * @param productCategoryPropertyDTO 入参
     * @param br 参数绑定
     * @return 结果
     * @throws Exception 业务异常
     */
    @RequestMapping(value = "/getCategoryProperty", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCategoryPropertory(@RequestBody @Valid ProductCategoryPropertyDTO productCategoryPropertyDTO, BindingResult br) throws Exception {
        XbbResponse<ProductCategoryPropertyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductCategoryPropertyVO productCategoryPropertyVO = productCategoryService.getCategoryProperty(productCategoryPropertyDTO);
                response = new XbbResponse<>(productCategoryPropertyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存分类属性设置
     * @param productCategoryPropertyDTO 入参
     * @param br 参数绑定
     * @return 结果
     * @throws Exception 业务异常
     */
    @RequestMapping(value = "/saveCategoryProperty", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveCategoryProperty(@RequestBody @Valid ProductCategoryPropertyDTO productCategoryPropertyDTO, BindingResult br) throws Exception {
        XbbResponse<ProductCategoryPropertyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProductCategoryPropertyVO productCategoryPropertyVO = productCategoryService.saveCategoryProperty(productCategoryPropertyDTO);
                response = new XbbResponse<>(productCategoryPropertyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 可采购商品
     * @param productCategoryListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/purchaseGoodsList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String purchaseGoodsList(@RequestBody @Valid ProductCategoryListDTO productCategoryListDTO, BindingResult br) throws Exception {
        XbbResponse<DepartmentSimpleListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DepartmentSimpleListVO departmentSimpleListVO = productCategoryService.purchaseGoodsList(productCategoryListDTO);
                response = new XbbResponse<>(departmentSimpleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
